import cv2
import numpy as np
from script.imageUtils import get_background_rect, get_bounding_rect, combine_image, cv2PIL, scale_image, save_filename


def putCenter(image_path, size=(800, 800), color=(255, 255, 255), scale=0.8):
    """透明图片获取最大举行放在正中位置"""
    image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
    image = get_bounding_rect(image)
    background = get_background_rect(size=cv2PIL(image).size, color=color)
    ref_image = combine_image(image, background, mask=True)
    background = get_background_rect(size=size)
    resize_image = scale_image(ref_image, max_size=size[0] * scale)
    h, w, n = resize_image.shape
    ref_image = combine_image(resize_image, background, position=(int((800 - w) / 2), int((800 - h) / 2)))
    filename = image_path.replace(".", "_center.")
    save_filename(ref_image, filename=filename)


if __name__ == '__main__':
    image = r"C:\Users\mengying\Desktop\workstation\modelscope-sanic\image_matting_done\54a56131d31c40efb9c825adc4b15696.png"
    putCenter(image)
